/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.standard;

import java.util.List;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.dusin.webcoal.exception.NoReplyException;
import org.just.dusin.ooii.component.Sink;
import org.dusin.webcoal.exception.OOIIException;

/**
 *
 * @author Administrator
 */
public class AMQWithReplySink implements Sink{
     private String brokerName="broke";
    private String brokerURL = "tcp://localhost:61616";
    private String queueName="coal";
    private String responseQueueName="response";
    private int  waitSecondsForReply=30;   //等待时间，超时将重新发送信息
    
    
    
    private String user;
    private String password;
    public String getBrokerName() {
        return brokerName;
    }

    public void setBrokerName(String brokerName) {
        this.brokerName = brokerName;
    }

    public String getBrokerURL() {
        return brokerURL;
    }

    public void setBrokerURL(String brokerURL) {
        this.brokerURL = brokerURL;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getResponseQueueName() {
        return responseQueueName;
    }

    public void setResponseQueueName(String responseQueueName) {
        this.responseQueueName = responseQueueName;
    }
    
    
    
    private static Logger logger = Logger.getLogger(AMQSink.class.getName());
    

    @Override
    public void initComponent(Properties props) {
        
    }

    @Override
    public void process(String msg) throws OOIIException {
        try{    
           
            ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);    	
            Connection connection = factory.createConnection(user,password);
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(null);
            Destination destination = session.createQueue(queueName );
            Message message = session.createTextMessage(msg); 
            
            Destination respDest = session.createQueue(responseQueueName);
            message.setJMSReplyTo(respDest);
            producer.send(destination, message);
            String id = message.getJMSMessageID();            
            logger.info(id);
            
            MessageConsumer replyConsumer = session.createConsumer(respDest);
            //接受应答
            boolean isReplyed = false;
            for(int i=0;i<waitSecondsForReply;i++){
                Thread.sleep(100);                
                TextMessage replyMessage = (TextMessage)replyConsumer.receive(700);
                if(replyMessage!=null&&replyMessage.getText().equals(id)){
                    logger.info("收到确认消息");
                    isReplyed = true;
                    break;
                }
            }
            if(replyConsumer!=null){
                replyConsumer.close();
            }
            if(producer!=null){
                producer.close();
            }
            if(session!=null){
                session.close();
            }
            if(connection != null) {
                connection.close();
            }
            if(!isReplyed)
                throw new NoReplyException(new Exception("no reply"));
        }catch(InterruptedException e){
            throw new NoReplyException(new Exception("no reply"));
        }catch(JMSException e){
            throw new OOIIException(e);
        }
    }

    @Override
    public void process(List<String> msgs) throws OOIIException{
    }

    @Override
    public void destroyComponent() throws Exception {
       
    }
}
